const SysAuthDao = appRequire(`dao/desktop/SysAuthSequelizeDao.js`);
const uuid = require('uuid');
var dateFormat = require("dateformat");
// const SqliteUtil = require('../../modules/SqliteUtil');
class SysAuthService {
    constructor() {

    }
    static async save(auth) {
        if (auth.id === '' || auth.id === undefined) {
            auth.id = uuid.v1();
            auth.createTime = dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss");
            auth.updateTime = auth.createTime;
            if(auth.path!=="/"){
                auth.path = auth.path+"/"+auth.id;
            }else{
                auth.path += auth.id;
            }
            await SysAuthDao.saveOne(auth);
            auth = await SysAuthDao.findOne(auth.id);
            return auth;
        } else {
            let dbObj = await SysAuthDao.findById(auth.id);
            // SqliteUtil.dbObjectToEntity(dbObj,auth);
            auth.updateTime = dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss");
            await SysAuthDao.updateOne(auth);
            auth = await SysAuthDao.findOne(auth.id);
            return auth;
        }
    }

    static async deleteOne(id) {
        return await SysAuthDao.deleteById(id);
    }

    static async deleteMany(ids) {
        ids = JSON.parse(ids);
        return await SysAuthDao.deleteMany(ids);
    }

    static async findOne(id) {
        return await SysAuthDao.findOne(id);
    }

    static async findAllUserAuths() {
        return await SysAuthDao.findAllUserAuths();
    }

    static async findAll() {
        return await SysAuthDao.findAll();
    }

    static async findByPage(page) {
        page.pageNum = parseInt(page.pageNum);
        page.pageSize = parseInt(page.pageSize);
        page.searchParams = JSON.parse(page.searchParams);
        return await SysAuthDao.findByPage(page);
    }

}
module.exports = SysAuthService;